package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.data.DataResult;
import com.dongdong.data.ResponseStatusEnum;
import com.dongdong.pojo.TbBrand;
import com.dongdong.pojo.TbSpecification;
import com.dongdong.pojo.TbTypeTemplate;
import com.dongdong.service.BrandService;
import com.dongdong.service.SpecificationService;
import com.dongdong.service.TemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Deacription TODO
 * @Author Lenovo
 * @Date 2020/12/10 22:03
 * @Version 1.0
 **/
@Controller
@RequestMapping("templateController")
public class TemplateController {

    private static final Logger logger = LoggerFactory.getLogger(TemplateController.class);

    @Reference
    private TemplateService templateService;

    @Reference
    private BrandService brandService;

    @Reference
    private SpecificationService specificationService;

    @RequestMapping("list")
    public String list(Model model){
        List<TbBrand> tbBrands = brandService.getBrandList();
        List<TbSpecification> tbSpecifications = specificationService.getSpecifications();
        model.addAttribute("brand",tbBrands);
        model.addAttribute("sf",tbSpecifications);
        return "admin/type_template";
    }

    @RequestMapping("selectTemplate")
    @ResponseBody
    public DataResult<List<TbTypeTemplate>> selectTemplate(@RequestParam("temName") String temName){
        try {
            List<TbTypeTemplate> list = templateService.selectTemplate(temName);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(list);
        }catch (Exception e){
            logger.error("查询失败");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("insertTemplate")
    @ResponseBody
    public DataResult insertTemplate(TbTypeTemplate tbTypeTemplate){
        try {
            int i = templateService.insertTemplate(tbTypeTemplate);
            return DataResult.response(ResponseStatusEnum.SUCCESS);
        }catch (Exception e){
            logger.error("增加失败");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("getTemplateById")
    @ResponseBody
    public DataResult updateTemplate(@RequestParam("id") Long id){
        try {
            TbTypeTemplate tbTypeTemplate = templateService.getTemplateById(id);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(tbTypeTemplate);
        }catch (Exception e){
            logger.error("回显失败");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("updateTemplate")
    @ResponseBody
    public DataResult updateTemplate(TbTypeTemplate tbTypeTemplate){
        try {
            int i = templateService.updateTemplate(tbTypeTemplate);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(i);
        }catch (Exception e){
            logger.error("修改错误");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("deleteBatch")
    @ResponseBody
    public DataResult deleteBatch(@RequestParam("ids[]") Long[] ids){
        try {
            int i = templateService.deleteBatch(ids);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(i);
        }catch (Exception e){
            logger.error("删除失败");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }
}

